/*
 * Created by JFormDesigner on Wed Nov 30 11:24:41 EET 2011
 */

package tr.gov.tubitak.bilgem.esya.certviewer;

import java.awt.event.*;
import javax.swing.border.*;

import tr.gov.tubitak.bilgem.esya.certviewer.actions.ECertExportToFileAction;
import tr.gov.tubitak.bilgem.esya.certviewer.viwerasn.*;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.*;
import java.security.cert.CertificateEncodingException;
import java.security.cert.X509Certificate;
import java.util.*;
import java.util.List;

/**
 * @author Haydar Karabas
 */
public class ECertDetailViewPanel extends JPanel {
    byte [] certData;
    public ECertDetailViewPanel(X509Certificate x509Certificate) {
        initComponents();
        eCertFieldTable.getSelectionModel().addListSelectionListener(new ECertTableSelectionListener());
        loadCert(x509Certificate);

    }

    void loadCert(X509Certificate x509Certificate)
    {
        ECertViewerCertificate cert = new ECertViewerCertificate(x509Certificate);
        List<ECertField> fieldList = cert.getFieldList();
        certFieldTableModel = new ECertFieldTableModel(fieldList);
        eCertFieldTable.setModel(certFieldTableModel);
        try {
            this.certData = x509Certificate.getEncoded();
        } catch (CertificateEncodingException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        btnExportToFile.setAction(new ECertExportToFileAction(certData));
    }

    static String getTitle()
    {
        return Bundle.getString("ECertDetailViewPanel.tabName");
    }

    private void eCertFieldTypeCombo1ActionPerformed(ActionEvent e) {
        Object selectedItem = eCertFieldTypeCombo1.getSelectedItem();
        if(selectedItem instanceof ECertField.ECertFieldType)
        {
            ECertField.ECertFieldType selectedTypeItem = (ECertField.ECertFieldType) selectedItem;
            eCertFieldTable.setFilterFieldType(selectedTypeItem);
        }
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        ResourceBundle bundle = Bundle.getBundle();
        panel1 = new JPanel();
        lblCertFieldShow = new JLabel();
        eCertFieldTypeCombo1 = new ECertFieldTypeCombo();
        panel2 = new JPanel();
        panel4 = new JPanel();
        scrollPane1 = new JScrollPane();
        eCertFieldTable = new ECertFieldTable();
        panel6 = new JPanel();
        lblFieldName = new JLabel();
        panel3 = new JPanel();
        scrollPane2 = new JScrollPane();
        txtExtensionDetail = new JTextPane();
        panel5 = new JPanel();
        btnExportToFile = new JButton();

        //======== this ========
        setLayout(new BorderLayout());

        //======== panel1 ========
        {
            panel1.setLayout(new FlowLayout(FlowLayout.LEFT));

            //---- lblCertFieldShow ----
            lblCertFieldShow.setText(bundle.getString("ECertDetailViewPanel.lblCertFieldShow.text"));
            panel1.add(lblCertFieldShow);

            //---- eCertFieldTypeCombo1 ----
            eCertFieldTypeCombo1.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    eCertFieldTypeCombo1ActionPerformed(e);
                }
            });
            panel1.add(eCertFieldTypeCombo1);
        }
        add(panel1, BorderLayout.PAGE_START);

        //======== panel2 ========
        {
            panel2.setLayout(new GridLayout(2, 0));

            //======== panel4 ========
            {
                panel4.setBorder(new EmptyBorder(5, 5, 5, 5));
                panel4.setLayout(new GridLayout());

                //======== scrollPane1 ========
                {

                    //---- eCertFieldTable ----
                    eCertFieldTable.setShowVerticalLines(false);
                    eCertFieldTable.setShowHorizontalLines(false);
                    eCertFieldTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
                    scrollPane1.setViewportView(eCertFieldTable);
                }
                panel4.add(scrollPane1);
            }
            panel2.add(panel4);

            //======== panel6 ========
            {
                panel6.setLayout(new BorderLayout());

                //---- lblFieldName ----
                lblFieldName.setText(bundle.getString("ECertDetailViewPanel.lblFieldName.text"));
                lblFieldName.setFont(lblFieldName.getFont().deriveFont(lblFieldName.getFont().getStyle() | Font.BOLD));
                panel6.add(lblFieldName, BorderLayout.PAGE_START);

                //======== panel3 ========
                {
                    panel3.setBorder(new EmptyBorder(5, 5, 5, 5));
                    panel3.setLayout(new GridLayout());

                    //======== scrollPane2 ========
                    {

                        //---- txtExtensionDetail ----
                        txtExtensionDetail.setEditable(false);
                        scrollPane2.setViewportView(txtExtensionDetail);
                    }
                    panel3.add(scrollPane2);
                }
                panel6.add(panel3, BorderLayout.CENTER);
            }
            panel2.add(panel6);
        }
        add(panel2, BorderLayout.CENTER);

        //======== panel5 ========
        {
            panel5.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
            panel5.setBorder(new EmptyBorder(5, 5, 5, 5));
            panel5.setLayout(new BoxLayout(panel5, BoxLayout.X_AXIS));

            //---- btnExportToFile ----
            btnExportToFile.setText(bundle.getString("ECertDetailViewPanel.btnExportToFile.text"));
            panel5.add(btnExportToFile);
        }
        add(panel5, BorderLayout.PAGE_END);
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    private void initComponentsI18n() {
        // JFormDesigner - Component i18n initialization - DO NOT MODIFY  //GEN-BEGIN:initI18n
        // JFormDesigner - End of component i18n initialization  //GEN-END:initI18n
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JPanel panel1;
    private JLabel lblCertFieldShow;
    private ECertFieldTypeCombo eCertFieldTypeCombo1;
    private JPanel panel2;
    private JPanel panel4;
    private JScrollPane scrollPane1;
    private ECertFieldTable eCertFieldTable;
    private JPanel panel6;
    private JLabel lblFieldName;
    private JPanel panel3;
    private JScrollPane scrollPane2;
    private JTextPane txtExtensionDetail;
    private JPanel panel5;
    private JButton btnExportToFile;
    // JFormDesigner - End of variables declaration  //GEN-END:variables

    ECertFieldTableModel certFieldTableModel;
    class ECertTableSelectionListener implements ListSelectionListener
    {

        @Override
        public void valueChanged(ListSelectionEvent e) {
            int index = eCertFieldTable.getSelectedRow();
            if(index<0)
            {
                return;
            }
            index = eCertFieldTable.convertRowIndexToModel(index);
            if(index<0)
            {
                return;
            }
            ECertField rowElement = certFieldTableModel.getRowElement(index);
            txtExtensionDetail.setText(rowElement.getDetailedValue());
            lblFieldName.setText("   "+rowElement.getTitle());
        }
    }
}
